<script type="text/javascript" language="javascript">

$(function(){
	var invoice_item_row_template=$.format($('#invoice-form-item-template').val());
	var invoice_olditem_row_template=$.format($('#invoice-form-olditem-template').val());
	var invoice_item_i = 1;

	function addInvoiceItemRow(item) {
		var newRow;
		if(item!=undefined){
			newRow=$(invoice_olditem_row_template(item.invoice_item_id,item.name,item.description,item.rate,item.quantity,item.row_total));
			newRow.data('item_id',item.invoice_item_id);
		} else {
			newRow=$(invoice_item_row_template(invoice_item_i));
			newRow.data('item_id',-1);
		}
		newRow.appendTo("#invoice_items_body");
		$('.row-rm-btn',newRow).button({
			icons: {
				primary: "ui-icon-trash"
			},
			text:false
		}).click(function(){
			var item_id = $(this).parent().parent().data('item_id');
			$.getJSON('<?=site_url("/backend/ajax/invoices/remove_item/")?>/'+item_id,
				function(data){
				}
			);
			$(this).parent().parent().remove();
		}).easyconfirm({
			locale:{
				title: "Are you sure you want to remove this Item?",
				text: "This action cannot be undone!"
			}
		});
		invoice_item_i++;
		return false;
	}

	$('#invoices_table').flexigrid({
		url: '<?=site_url("backend/ajax/invoices/get")?>',
		dataType: 'json',
		colModel : [
			{display: 'Invoice Id', name : 'invoice_id', width : 100, sortable : true, align: 'left'},
			{display: 'Client Name', name : 'name', width : 180, sortable : true, align: 'left'},
			{display: 'Issue Date', name : 'issue_date', width : 120, sortable : true, align: 'left'},
			{display: 'Subtotal', name : 'subtotal', width : 130, sortable : true, align: 'left'},
			{display: 'Paid', name : 'paid', width : 80, sortable : true, align: 'right'},
			{display: 'Balance', name : 'balance', width : 80, sortable : true, align: 'right'},
			{display: 'Mailing Address', name : 'mailing_address', width : 80, sortable : true, align: 'right', hide: true},
			{display: 'Email Address', name : 'email_address', width : 80, sortable : true, align: 'right', hide: true},
			{display: 'Notes', name : 'notes', width : 80, sortable : true, align: 'right', hide: true}
			],
		buttons : [
			{name: 'Add', bclass: 'ui-icon-document', options:{icons:{primary: 'ui-icon-document'}}, onpress : function(){
				$('#invoice-form').resetForm();
				$("#invoice_client_id").val('').trigger('change').trigger("liszt:updated");
				$('#invoice_items_body').children().remove();
				$('#invoice-dialog-form').dialog('open');
				addInvoiceItemRow();
			}},
			{name: 'Edit', bclass: 'ui-icon-pencil', options:{icons:{primary: 'ui-icon-pencil'}}, onpress : function(act, grid){
				$('.trSelected', grid).each(function(index, row) {
					var row_data = $(row).data('rowData');
					var row_id = row_data.invoice_id;
					$.getJSON('<?=site_url("/backend/ajax/invoices/get/")?>/'+row_id,
						function(data){
							thisInvoice = data.data[0];
							$('#invoice_act').val("mod");
							$("#invoice_client_id").val(thisInvoice.client_id).trigger('change').trigger("liszt:updated");
							$("#invoice_address").val(thisInvoice.client_mailing_address);
							$("#invoice_id").val(thisInvoice.invoice_id);
							$("#invoice_issue_date").val(thisInvoice.issue_date);
							$("#invoice_discount").val(thisInvoice.discount);
							$("#invoice_items_body").children().remove();
							$.each(thisInvoice.items,function(index,item){
								addInvoiceItemRow(item);
							});
							$("#invoice_subtotal").val(thisInvoice.subtotal);
							$("#invoice_taxes").val(thisInvoice.taxes);
							$("#invoice_paid").val(thisInvoice.paid);
							$("#invoice_total").val(thisInvoice.balance);
							$("#invoice_terms").val(thisInvoice.terms);
							$("#invoice_notes").val(thisInvoice.notes);
							$('#invoice-dialog-form').dialog('open');
						}
					);
				});
				
			}}/*,
			{name: 'Delete', bclass: 'ui-icon-trash', options:{icons:{primary: 'ui-icon-trash'}}}*/
			],
		searchitems : [
			{display: 'Invoice Id', name : 'invoice_id'},
			{display: 'Name', name : 'name', isdefault: true}
			],
		sortname: "invoice_id",
		sortorder: "asc",
		usepager: true,
		useRp: true,
		rp: 15,
		height: 200,
		themeRoller:true,
		singleSelect:true
	});

	$('#invoice-form-add-itm-btn').button({
		icons: {
			primary: "ui-icon-plus"
		}
	}).click(function(){
		addInvoiceItemRow();
	});

	$( "#invoice-dialog-form" ).dialog({
		autoOpen: false,
		minWidth: 800,
		maxWidth: 800,
		width:800,
		modal: true,
		buttons: {
			"Save": function() {
				var data=$('#invoice-form').serialize();
				$.ajax({
					url:'<?=site_url("/backend/ajax/invoices/save")?>', 
					data: data,
					dataType: 'json',
					type: 'POST',
					success: function(rData,textStatus,jqXHR) {
						if(rData.status=='success'){
							alert(rData.msg);
							$('#invoice-dialog-form').dialog('close');
						} else {
							alert("ERROR: \r\n"+rData.msg);
						}
					}
				});
			},
			Cancel: function() {
				$( this ).dialog( "close" );
			}
		},
		close: function() {
			$('#invoice-form').resetForm();
			$("#invoice_client_id").val(thisInvoice.client_id).trigger('change').trigger("liszt:updated");
			$('#invoice_items_body').children().remove();
			$('#invoice_act').val("add");
		}
	});
});

</script>
